Fix zero-repetition measurement record shapes in simulator - #8316
Fix zero-repetition measurement record shapes in simulator#8316Prahalad-ship-it wants to merge 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
arettig
left a comment
There was a problem hiding this comment.
Thanks for doing this! I have a recommendation to simplify this code, see below.
| for _, op, _ in program.findall_operations_with_gate_type(ops.MeasurementGate): | ||
| records[protocols.measurement_key_name(op)] = np.empty([0, 1, 1]) | ||
| key = protocols.measurement_key_name(op) | ||
| qid_shape = protocols.qid_shape(op) | ||
| if key in record_shapes: | ||
| num_instances, expected_qid_shape = record_shapes[key] | ||
| if qid_shape != expected_qid_shape: | ||
| raise ValueError( | ||
| 'Different qid shapes for repeated measurement: ' | ||
| f'key={key!r}, prev_qid_shape={expected_qid_shape}, ' | ||
| f'qid_shape={qid_shape}' | ||
| ) | ||
| record_shapes[key] = (num_instances + 1, qid_shape) | ||
| else: | ||
| record_shapes[key] = (1, qid_shape) |
There was a problem hiding this comment.
We should just call _get_measurement_shapes here from the Sampler parent class, as is done in zeros_sampler.py.
| result = cirq.Simulator().run(circuit, repetitions=0) | ||
|
|
||
| assert result.repetitions == 0 | ||
| assert result.records['m'].shape == (0, 2, 2) |
There was a problem hiding this comment.
Please add a test case where to number of measurements is different than the number of qubits.
Summary
This PR fixes an issue in Cirq’s sampling interface where the simulator returned incorrectly shaped empty measurement records when circuits were run with
repetitions=0.Previously, the zero-repetition path bypassed execution but hardcoded an empty record shape of
(0, 1, 1). This occurred regardless of how many qubits were measured or how many times a key was repeated.This change updates the zero-repetition path so that
Result.recordsaccurately preserves the circuit's structural dimensions:(repetitions, measurement_instances, measured_qubits)->(0, measurement_instances, measured_qubits)Problem
Cirq’s
SimulatesSamples.run_sweep_itermethod includes a fast path for zero repetitions to avoid unnecessary simulation. However, it still must construct a validResultDictcontaining accurately dimensioned empty measurement records.Before this change, the implementation initialized these records using
np.empty([0, 1, 1]). This fallback shape failed to represent circuits utilizing multi-qubit measurement operations or repeated measurement keys.Minimal Reproducible Example
Previous Behavior
Corrected Behavior
02"m"occurs twice in the circuit2q0,q1)Note: This is a correctness fix, not a performance optimization. No unverified simulator optimizations have been introduced.
Tests Added
Added a dedicated regression test in
simulator_test.pythat constructs a circuit containing multiple qubits and repeated measurement keys. The test runs withrepetitions=0and asserts:Prior to this fix, the shape assertion would fail with a layout mismatch.
Validation Performed
The following checks passed locally:
ruffchecks passed successfully on all modified files.cirq-core/cirq/sim.Files Changed
cirq-core/cirq/sim/simulator.pyUpdated the zero-repetition execution pipeline to:
cirq-core/cirq/sim/simulator_test.pyAdded regression coverage validating repeated multi-qubit measurement outputs under zero repetitions.
API Compatibility
This change is fully backwards-compatible.